// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.system.controller;

import com.liuxinlong.annotation.LogAnnotation;
import com.liuxinlong.common.ImportResp;
import com.liuxinlong.common.UploadService;
import com.liuxinlong.constant.LogBusinessConstants;
import com.liuxinlong.constant.LogOperateConstants;
import com.liuxinlong.modules.common.controller.request.BaseDeleteReq;
import com.liuxinlong.modules.common.controller.request.QueryBaseReq;
import com.liuxinlong.modules.common.controller.response.BaseResp;
import com.liuxinlong.modules.entity.UserGroup;
import com.liuxinlong.modules.system.controller.request.ImportGroupMemberReq;
import com.liuxinlong.modules.system.controller.request.ImportUserGroupReq;
import com.liuxinlong.modules.system.controller.request.QueryUserReq;
import com.liuxinlong.modules.system.controller.request.UpdateGroupMemberReq;
import com.liuxinlong.modules.system.controller.request.UpdateUserGroupReq;
import com.liuxinlong.modules.system.service.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户分组信息控制层
 * 
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2022-07-04
 */
@RestController
@RequestMapping("/group")
public class GroupController {

	@Autowired
	private GroupService groupService;

	@Autowired
	private UploadService uploadService;

	/**
	 * 分页查询用户分组信息
	 * 
	 * @param req 请求信息
	 * @return 用户分组信息列表
	 */
	@PostMapping("page")
	@LogAnnotation(value ="分页查询",business = LogBusinessConstants.GROUP_MANAGE,operateType = LogOperateConstants.OPERATE_QUERY)
	public BaseResp pageUserGroup(@RequestBody QueryUserReq req) {
		Map<String, Object> queryMap = new HashMap<>();
		queryMap.put("startNum", req.getStartNum());
		queryMap.put("pageSize", req.getPageSize());
		queryMap.put("name", req.getName());
		List<Map<String, Object>> dataList = groupService.pageUserGroup(queryMap);
		int total = groupService.countUserGroup(queryMap);
		BaseResp resp = new BaseResp();
		resp.setRetMessage("分页查询用户分组信息成功");
		resp.setDataList(dataList);
		resp.setTotalNum(total);
		return resp;
	}

	/**
	 * 新增用户分组信息
	 * 
	 * @param req 请求信息
	 * @return 成功标识
	 */
	@PostMapping("add")
	@LogAnnotation(value ="新增用户分组信息",business = LogBusinessConstants.GROUP_MANAGE,operateType = LogOperateConstants.OPERATE_ADD)
	public BaseResp addUserGroup(@RequestBody UpdateUserGroupReq req) {
		UserGroup userGroup = new UserGroup();
		userGroup.setName(req.getName());
		userGroup.setSn(req.getSn());
		userGroup.setFolderPath(req.getFolderPath());
		userGroup.setRemark(req.getRemark());
		groupService.addUserGroup(userGroup);
		BaseResp resp = new BaseResp();
		resp.setRetMessage("新增用户分组信息成功");
		return resp;
	}

	/**
	 * 修改用户分组信息
	 * 
	 * @param req 请求信息
	 * @return 成功标识
	 */
	@PostMapping("update")
	@LogAnnotation(value ="修改用户分组信息",business = LogBusinessConstants.GROUP_MANAGE,operateType = LogOperateConstants.OPERATE_MODIFY)
	public BaseResp updateUserGroup(@RequestBody UpdateUserGroupReq req) {
		UserGroup userGroup = new UserGroup();
		userGroup.setId(req.getId());
		userGroup.setName(req.getName());
		userGroup.setSn(req.getSn());
		userGroup.setFolderPath(req.getFolderPath());
		userGroup.setRemark(req.getRemark());
		groupService.updateUserGroup(userGroup);
		BaseResp resp = new BaseResp();
		resp.setRetMessage("修改用户分组信息成功");
		return resp;
	}

	/**
	 * 删除用户分组信息
	 * 
	 * @param req 请求信息
	 * @return 成功标识
	 */
	@PostMapping("delete")
	@LogAnnotation(value ="删除用户分组信息",business = LogBusinessConstants.GROUP_MANAGE,operateType = LogOperateConstants.OPERATE_DELETE)
	public BaseResp deleteUserGroup(@RequestBody BaseDeleteReq req) {
		groupService.deleteUserGroup(req.getId());
		BaseResp resp = new BaseResp();
		resp.setRetMessage("删除用户分组信息成功");
		return resp;
	}

	/**
	 * 导入数据预处理
	 *
	 * @param multipartRequest 文件
	 * @return 数据
	 */
	@PostMapping("import")
	@LogAnnotation(value ="导入用户分组数据",business = LogBusinessConstants.GROUP_MANAGE,operateType = LogOperateConstants.OPERATE_IMPORT)
	public ImportResp importUserGroup(MultipartRequest multipartRequest) {
		Map<String, Object> map = uploadService.uploadFileLocal(multipartRequest);
		ImportResp resp = groupService.importUserGroup((String) map.get("fileRealPath"));
		return resp;
	}

	/**
	 * 导入用户分组信息
	 *
	 * @param req 请求体
	 * @return 返回导入失败数据
	 */
	@PostMapping("batch-add")
	@LogAnnotation(value ="导入用户分组数据",business = LogBusinessConstants.GROUP_MANAGE,operateType = LogOperateConstants.OPERATE_IMPORT)
	public ImportResp batchAddUserGroup(@RequestBody ImportUserGroupReq req) {
		return groupService.batchAddUserGroup(req.getDataList());
	}

	/**
	 * 分页查询用户组成员信息
	 *
	 * @param req 请求信息
	 * @return 用户分组信息列表
	 */
	@PostMapping("/member/page")
	@LogAnnotation(value ="分页查询用户组成员",business = LogBusinessConstants.GROUP_MANAGE,operateType = LogOperateConstants.OPERATE_QUERY)
	public BaseResp pageGroupMember(@RequestBody QueryBaseReq req) {
		Map<String, Object> queryMap = new HashMap<>();
		queryMap.put("startNum", req.getStartNum());
		queryMap.put("pageSize", req.getPageSize());
		queryMap.put("groupId", req.getId());
		List<Map<String, Object>> dataList = groupService.pageGroupMember(queryMap);
		int total = groupService.countGroupMember(queryMap);
		BaseResp resp = new BaseResp();
		resp.setRetMessage("分页查询用户组成员信息成功");
		resp.setDataList(dataList);
		resp.setTotalNum(total);
		return resp;
	}

	/**
	 * 修改用户组成员信息
	 *
	 * @param req 请求信息
	 * @return 成功标识
	 */
	@PostMapping("/member/update")
	@LogAnnotation(value ="修改用户组成员信息",business = LogBusinessConstants.GROUP_MANAGE,operateType = LogOperateConstants.OPERATE_MODIFY)
	public BaseResp updateGroupMembers(@RequestBody UpdateGroupMemberReq req) {
		groupService.updateGroupMembers(req.getGroupId(),req.getDataList());
		BaseResp resp = new BaseResp();
		resp.setRetMessage("修改用户组成员信息成功");
		return resp;
	}

	/**
	 * 导入数据预处理
	 *
	 * @param multipartRequest 文件
	 * @return 数据
	 */
	@PostMapping("member/import")
	@LogAnnotation(value ="导入用户分组关联数据",business = LogBusinessConstants.GROUP_MANAGE,operateType = LogOperateConstants.OPERATE_IMPORT)
	public ImportResp importGroupMember(MultipartRequest multipartRequest) {
		Map<String, Object> map = uploadService.uploadFileLocal(multipartRequest);
		ImportResp resp = groupService.importGroupMember((String) map.get("fileRealPath"));
		return resp;
	}

	/**
	 * 导入用户分组关联信息
	 *
	 * @param req 请求体
	 * @return 返回导入失败数据
	 */
	@PostMapping("member/batch-add")
	@LogAnnotation(value ="导入用户分组关联数据",business = LogBusinessConstants.GROUP_MANAGE,operateType = LogOperateConstants.OPERATE_IMPORT)
	public ImportResp batchAddGroupMember(@RequestBody ImportGroupMemberReq req) {
		return groupService.batchAddGroupMember(req.getDataList());
	}

	/**
	 * 导出用户分组信息
	 *
	 * @return 导出数据
	 */
	@PostMapping("export")
	@LogAnnotation(value ="导出用户分组信息",business = LogBusinessConstants.GROUP_MANAGE,operateType = LogOperateConstants.OPERATE_EXPORT)
	public void exportGroupInfo(HttpServletResponse response) {
		groupService.exportGroupInfo(response);
	}

}
